package com.android.phoenix.activity.drawLine;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.android.phoenix.MainApplication;
import com.android.phoenix.R;
import com.android.phoenix.activity.SportRecordDetailActivity;
import com.android.phoenix.models.SportRecord;
import com.android.phoenix.util.Ray_FileUtils;
import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class SportDrawLine extends Activity {

	private GraphView graph_view_speed;
	private GraphView graph_view_altitude;
	private GraphView graph_view_heartrate;

	private TextView txt_minspeed;
	private TextView txt_averagespeed;
	private TextView txt_maxspeed;

	private TextView txt_minaltitude;
	private TextView txt_averagealtitude;
	private TextView txt_maxaltitude;

	private TextView txt_minheart;
	private TextView txt_averageheart;
	private TextView txt_maxheart;

	private int startTime = 0;
	private int endTime = 0;
	private String path = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_sport_draw_line);
		initView();

	}

	private void initView() {
		graph_view_speed = (GraphView) this.findViewById(R.id.graph_view_speed);
		graph_view_altitude = (GraphView) this
				.findViewById(R.id.graph_view_altitude);
		graph_view_heartrate = (GraphView) this
				.findViewById(R.id.graph_view_heartrate);

		path = getIntent().getExtras().getString("path");

		if (path == null) {
			Toast.makeText(SportDrawLine.this, "读取记录失败！", Toast.LENGTH_LONG)
					.show();
			finish();
			return;
		}

		txt_minspeed = (TextView) this.findViewById(R.id.txt_minspeed);
		txt_averagespeed = (TextView) this.findViewById(R.id.txt_averagespeed);
		txt_maxspeed = (TextView) this.findViewById(R.id.txt_maxspeed);

		txt_minaltitude = (TextView) this.findViewById(R.id.txt_minaltitude);
		txt_averagealtitude = (TextView) this
				.findViewById(R.id.txt_averagealtitude);
		txt_maxaltitude = (TextView) this.findViewById(R.id.txt_maxaltitude);

		txt_minheart = (TextView) this.findViewById(R.id.txt_minheart);
		txt_averageheart = (TextView) this.findViewById(R.id.txt_averageheart);
		txt_maxheart = (TextView) this.findViewById(R.id.txt_maxheart);

		readDataFromfile(path);

	}

	private void initGraphView(GraphView graphView, String[] strs) {
		LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>();
		float durTime = (endTime - startTime) / 1.0f / strs.length;
		Double maxY = 0d;
		Double minY = 0d;
		for (int i = 0; i < strs.length; i++) {
			series.appendData(
					new DataPoint(durTime * i + startTime, Double
							.parseDouble(strs[i])), true, 24);
			if (maxY < Double.parseDouble(strs[i])) {
				maxY = Double.parseDouble(strs[i]);
			}
			if (minY > Double.parseDouble(strs[i])) {
				minY = Double.parseDouble(strs[i]);
			}
		}
		graphView.getViewport().setMinX(startTime);
		graphView.getViewport().setMaxX(endTime);
		graphView.getViewport().setMaxY(maxY * (1.5));
		graphView.getViewport().setMinY(minY);
		graphView.getViewport().setYAxisBoundsManual(true);
		graphView.getViewport().setXAxisBoundsManual(true);

		switch (graphView.getId()) {
		case R.id.graph_view_speed:
			graphView.setTitle("速度详情");
			graphView.getGridLabelRenderer().setLabelFormatter(
					new DefaultLabelFormatter() {
						@Override
						public String formatLabel(double value, boolean isValueX) {
							if (isValueX) {
								// show normal x values
								return super.formatLabel(value, isValueX) + "h";
							} else {
								// show currency for y values
								return super.formatLabel(value, isValueX)
										+ "KM/h";
							}
						}
					});
			break;

		case R.id.graph_view_altitude:
			graphView.setTitle("海拔详情");
			graphView.getGridLabelRenderer().setLabelFormatter(
					new DefaultLabelFormatter() {
						@Override
						public String formatLabel(double value, boolean isValueX) {
							if (isValueX) {
								// show normal x values
								return super.formatLabel(value, isValueX) + "h";
							} else {
								// show currency for y values
								return super.formatLabel(value, isValueX) + "M";
							}
						}
					});
			break;
		case R.id.graph_view_heartrate:
			graphView.setTitle("心率详情");
			graphView.getGridLabelRenderer().setLabelFormatter(
					new DefaultLabelFormatter() {
						@Override
						public String formatLabel(double value, boolean isValueX) {
							if (isValueX) {
								// show normal x values
								return super.formatLabel(value, isValueX) + "h";
							} else {
								// show currency for y values
								return super.formatLabel(value, isValueX)
										+ "bpm";
							}
						}
					});
			break;
		}

		graphView.addSeries(series);

	}

	private void readDataFromfile(String path) {
		SportRecord record = new SportRecord();
		try {
			record = MainApplication.getDataHelper(SportDrawLine.this)
					.getSportRecordDao().queryForId(path);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (record == null) {
			Toast.makeText(SportDrawLine.this, "读取信息失败", 1).show();
			finish();
			return;
		}
		// 设置参数
		setParam(record);
		// 获取小时
		startTime = Integer.parseInt(record.getStartTime().substring(0, 2));
		endTime = Integer.parseInt(record.getEndTime().substring(0, 2)) + 1;

		// 15,18,20,22,13;50,22,60,50,45;90,120,130,140,150;
		// 速度，速度，速度，速度；海拔，海拔，海拔，海拔；心率，心率，心率，心率；
		String totalStr = Ray_FileUtils.readStringFromFile(path + "a");
		if (totalStr != null) {
			String[] strArray = totalStr.split(";");
			// 15,18,20,22,13
			if (strArray.length >= 3) {
				String[] speedStr = strArray[0].split(",");
				String[] AltitudeStr = strArray[1].split(",");
				String[] HeartStr = strArray[2].split(",");
				initGraphView(graph_view_speed, speedStr);
				initGraphView(graph_view_altitude, AltitudeStr);
				initGraphView(graph_view_heartrate, HeartStr);
			}
		} else {
			Toast.makeText(SportDrawLine.this, "读取信息失败", 1).show();
			finish();
		}

	}

	private void setParam(SportRecord record) {
		txt_minaltitude.setText(record.getMinAltitude());
		txt_minheart.setText(record.getMinHeartRate());

		txt_maxspeed.setText(record.getMaxSpeed());
		txt_maxheart.setText(record.getMaxHeartRate());
		txt_maxaltitude.setText(record.getMaxAltitude());
	}
}
